//
//  Introduce.m
//  ProjectA
//
//  Created by lanouhn on 15/6/4.
//  Copyright (c) 2015年 lanouhn. All rights reserved.
//

#import "Introduce.h"
#import "CustomTabBarController.h"
#define TotalRect [UIScreen mainScreen].bounds.size

@interface Introduce ()<UIScrollViewDelegate>

@end

@implementation Introduce
- (void)dealloc
{
    [_aScrollView release];
    [_pageControl release];
    [super dealloc];
}
- (UIScrollView *)aScrollView{
    if (!_aScrollView) {
        
        //创建scroller
        self.aScrollView = [[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, TotalRect.width, TotalRect.height)]autorelease];
        _aScrollView.contentSize = CGSizeMake(TotalRect.width * 4, TotalRect.height);
        [self addSubview:_aScrollView];
        
        NSArray *images = @[@"user1.jpg",@"user2.jpg",@"user3.jpg",@"user4.jpg"];
        for (int i = 0; i < 4; i ++) {
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i * CGRectGetWidth(_aScrollView.bounds), 0, CGRectGetWidth(_aScrollView.bounds), CGRectGetHeight(_aScrollView.bounds))];
            imageView.tag = i+10;
            imageView.image = [UIImage imageNamed:images[i]];
            [_aScrollView addSubview:imageView];
            [imageView release];
        }
        _aScrollView.pagingEnabled = YES;//整页翻动
        //？？？？？？？？？？
        //        aScrollView.contentOffset = CGPointMake(CGRectGetWidth(aScrollView.bounds), 0);
        _aScrollView.scrollEnabled= YES;//边界回弹动画//？？？？看不出来
        _aScrollView.showsHorizontalScrollIndicator = NO;//横向滑动指示器不显示
        _aScrollView.tag = 101;
        _aScrollView.bounces = NO;
        _aScrollView.delegate = self;
        //**********设置UIpa个Control*********
        
    
    }
    return _aScrollView;
}

- (UIPageControl *)pageControl{
    if (!_pageControl) {
        self.pageControl = [[[UIPageControl alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(_aScrollView.bounds) - 80, CGRectGetWidth(_aScrollView.bounds), 40)]autorelease];
        _pageControl.numberOfPages = 4;
        _pageControl.currentPage = 0;
        _pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
        _pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
        [self addSubview:_pageControl];
        
        _pageControl.tag = 112;
        [_pageControl addTarget:self action:@selector(handlePageControl:) forControlEvents:UIControlEventValueChanged];
    }
    return _pageControl;
}

- (void)handlePageControl:(UIPageControl *)sender{
    CGPoint offSet = CGPointMake(sender.currentPage *CGRectGetWidth(self.aScrollView.bounds), 0);
    self.aScrollView.contentOffset = offSet;
    if(self.aScrollView.contentOffset.x / CGRectGetWidth(self.aScrollView.bounds) == 3){
        [UIView animateWithDuration:5 animations:^{
            [self.aScrollView removeFromSuperview];
            [self.pageControl removeFromSuperview];
        }];
        
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSLog(@"fdasfsdf");
    NSInteger index = scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width;
       _pageControl.currentPage = index;
    
    if (index == 3) {
        [UIView animateWithDuration:5 animations:^{
            [self.aScrollView removeFromSuperview];
            [self.pageControl removeFromSuperview];
        }];
        
    }
    
}


@end
